var basePath = getBasePath();
//1.对传感器名称进行格式化
function sensorNameFormat(sensorName) {
    let sensorNameShow;
    if (sensorName.startsWith("T") || sensorName.startsWith("环温")) {
        sensorNameShow = sensorName + "(℃)";
    } else if ("电压" == sensorName) {
        sensorNameShow = sensorName + "(V)";
    } else if ("电流" == sensorName) {
        sensorNameShow = sensorName + "(A)";
    } else if ("功率" == sensorName) {
        sensorNameShow = sensorName + "(W)";
    } else if ("耗电量" == sensorName) {
        sensorNameShow = sensorName + "(kw.h)";
    } else if ("频率" == sensorName) {
        sensorNameShow = sensorName + "(Hz)";
    } else if ("湿温" == sensorName) {
        sensorNameShow = sensorName + "(℃)";
    } else {
        sensorNameShow = sensorName;
    }
    return sensorNameShow;

}

//2.获取所有选择的传感器名称
function getAllSelectSensorName(primaryKey,testunitno) {
    $.ajax({
        type: "get",
        url: "http://localhost/consumer/dataListAPI/getAllSelectSensorName",
        data:{
            primaryKey:primaryKey,
            testunitno:testunitno
        },
        success: function (msg) {
            var item = "<thead>" + "<tr>" +
                "<td><strong>日期</strong></td>"+ "<td><strong>时间</strong></td>"+"<td><strong>测试时长</strong></td>";
            $("#table_content tr").eq(1).remove();
            for (var i = 0; i < msg.length; i++) {
                //拼接字符串
                item = item + "<td><strong>" + sensorNameFormat(msg[i].name) + "</strong></td>";
            }
            item = item + "</tr>" + "</thead>";
            $("#table_content").append(item);
        },
        error: function (e) {
            alert("网页发生错误：", e, "error");
        }
    });
}
//浮点数乘法
function accMul(arg1,arg2)
{
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

//3.获取数据列表
function getDataList(labcode,testunitno,pageNum,startdate,enddate) {
    let layerid=layer.msg('加载中', {
        icon: 16,
        shade: 0.4,
        time:false //取消自动关闭
    });
    $.ajax({
        type: "get",
        url: "http://localhost/consumer/dataListAPI/getAllTestDataVo",
        data:{
            labcode:labcode,
            testunitno:testunitno,
            pageNum:pageNum,
            startdate:startdate,
            enddate:enddate
        },
        beforeSend : function(){
            console.log("发送请求准备");
        },
        success: function (msg) {
            layer.close(layerid);//手动关闭
            var item = "<tbody>";
            //$("#table_content tr:not(:first)").remove();
            $("#table_content tbody tr").remove();
            //遍历数据的另一种方式
            // $.each(msg.list, function(i, user){
            //
            // });
            for (var i = 0; i < msg.list.length; i++) {
                //传感器编号列表
                var sensorNoList = msg.list[i].sensorNoList;
                //传感器名称列表
                var sensorNameList = msg.list[i].sensorNameList;
                //传感器数据列表
                var sensorValueList = msg.list[i].sensorValueList;
                    //拼接字符串 class='info'
                var time = msg.list[i].time;
                var timeFormat = time.split(".");
                var testLongTime = msg.list[i].howlong;
                var long = accMul( parseFloat(testLongTime),60);
                console.log(long);
                var mathRound = Math.round(long);
;
                if (sensorValueList.length==2){
                    item = item + "<tr style='background-color: red'>" +
                        "<td>" + msg.list[i].date + "</td>" +
                        "<td>" + timeFormat[0] + "</td>" +
                        "<td>" +mathRound + "</td>";
                    //拼接传感器的值
                    for (var j = 0; j < 92; j++) {
                        if (j<2){
                            item = item + "<td>" + sensorValueList[j] + "</td>";
                        } else{
                            item = item + "<td>" + "" + "</td>";
                        }

                    }
                }else{
                    item = item + "<tr>" +
                        "<td>" + msg.list[i].date + "</td>" +
                        "<td>" + timeFormat[0] + "</td>"+
                        "<td>" + mathRound + "</td>";
                    //拼接传感器的值
                    for (var j = 0; j < sensorValueList.length; j++) {
                        item = item + "<td>" + sensorValueList[j] + "</td>";
                    }
                }
                    item = item + "</tr>";


             /*   else {
                    //拼接字符串
                    item = item + "<tr>" +
                        "<td>" + msg.list[i].date + "</td>" +
                        "<td>" + msg.list[i].time + "</td>";
                    //拼接传感器的值
                    for (var j = 0; j < sensorValueList.length; j++) {
                        if (sensorValueList.length==92){
                            item = item + "<td>" + sensorValueList[j] + "</td>";
                        }else{
                            item = item + "<td>" + sensorValueList[j] + "</td>";
                            for(var ii=0;ii<92-sensorValueList.length;ii++){
                                item = item + "<td>" + "" + "</td>";
                            }
                        }

                    }
                    item = item + "</tr>";
                }*/

            }
            item = item + "</tbody>";
            $("#table_content").append(item);
            var pageNum = msg.pageNum;
            var total = msg.total;
            var pages= msg.pages;
            //设置页码
            $("#total").text(total);
            $("#pageNum").text(pageNum);
            $("#pages").text(pages);
        },
        error: function (e) {
            alert("网页发生错误：", e, "error");
        }
    });
}